from ctypes import *
import os

class PyAgDongle2(): #软件加密狗
    def __init__(self):
        self.dll_path = os.path.dirname(__file__)+b"\CAgDongle_RD2_64.dll".decode("utf-8")
        print(self.dll_path)
        try:
            self._dll = windll.LoadLibrary(self.dll_path)
            print("PyAgDongle2 DLL 加载成功。")
        except OSError as e:
            print(f"PyAgDongle2 无法加载 DLL: {e}")


        # agVCEncode 函数接受一个指向 unsigned char 的指针，并返回 None
        # 例如，为 agCreateCAgDongle2 函数定义原型
        self._dll.agCreateCAgDongle2.argtypes = []
        self._dll.agCreateCAgDongle2.restype = c_int

        # 为 agDeleteCAgDongle2 函数定义原型
        self._dll.agDeleteCAgDongle2.argtypes = []
        self._dll.agDeleteCAgDongle2.restype = None

        # 为 agAuthorityValidate2 函数定义原型
        self._dll.agAuthorityValidate2.argtypes = []
        self._dll.agAuthorityValidate2.restype = c_int

        # 为 agGetAuthorityCode2 函数定义原型
        self._dll.agGetAuthorityCode2.argtypes = []
        self._dll.agGetAuthorityCode2.restype = c_uint

        # 为 agGenerateMRCode_DR2 函数定义原型
        self._dll.agGenerateMRCode_DR2.argtypes = [POINTER(c_char), POINTER(c_char)]
        self._dll.agGenerateMRCode_DR2.restype = c_int

        # 为 agActivateSoftware2 函数定义原型
        self._dll.agActivateSoftware2.argtypes = [c_char_p]
        self._dll.agActivateSoftware2.restype = c_int
    
    def py_agCreateCAgDongle2(self):
        return self._dll.agCreateCAgDongle2()


    def py_agDeleteCAgDongle2(self):
        self._dll.agDeleteCAgDongle2()

    def py_agAuthorityValidate2(self):
        validate =  self._dll.agAuthorityValidate2()
        print(validate)
        return validate

    def py_agGetAuthorityCode2(self):
        return self._dll.agGetAuthorityCode2()

    def py_agGenerateMRCode_DR2(self):
        cspMRCode = create_string_buffer(b'\x00' * 33)  # 33 字节，包括空字符
        csMachineID = create_string_buffer(b'\x00' * 14)  # 14 字节，包括空字符

        r = self._dll.agGenerateMRCode_DR2(cspMRCode, csMachineID)
        if r == 0:
            print(f"\r\n机器码: {cspMRCode.value.decode()}")
            print(f"机器ID: {csMachineID.value.decode()}")
            return cspMRCode.value.decode(), csMachineID.value.decode()
        else:
            print(f"函数调用失败，错误码：{r}")


    def py_agActivateSoftware2(self, RegisterKey):
        RegisterKey = c_char_p(str(RegisterKey).encode('utf-8'))    
        r = self._dll.agActivateSoftware2(RegisterKey)
        print(r)
        return r

class PyAgDongle(): #硬件加密狗
    def __init__(self):
        self.dll_path = os.path.dirname(__file__)+b"\CAgDongle_RD1_64.dll".decode("utf-8")
        print(self.dll_path)
        try:
            self._dll = windll.LoadLibrary(self.dll_path)
            print("PyAgDongle1 DLL 加载成功。")
        except OSError as e:
            print(f"PyAgDongle1 无法加载 DLL: {e}")

        self._dll.agCreateCAgDongle.argtypes = []
        self._dll.agCreateCAgDongle.restype = c_int
       
        self._dll.agDeleteCAgDongle.argtypes = []
        self._dll.agDeleteCAgDongle.restype = None
       
        self._dll.agAuthorityValidate.argtypes = []
        self._dll.agAuthorityValidate.restype = c_int

        self._dll.agGetAuthorityCode.argtypes = []
        self._dll.agGetAuthorityCode.restype = c_uint

        self._dll.agGetLocalMachineID.argtypes = [POINTER(c_char)]
        self._dll.agGetLocalMachineID.restype = c_int

        self._dll.agGenerateMRCode_DR.argtypes = [POINTER(c_char)]
        self._dll.agGenerateMRCode_DR.restype = c_int

        self._dll.agActivateSoftware.argtypes = [c_char_p]
        self._dll.agActivateSoftware.restype = c_int

    def py_agCreateCAgDongle(self):
        return self._dll.agCreateCAgDongle()


    def py_agDeleteCAgDongle(self):
        self._dll.agDeleteCAgDongle()

    def py_agAuthorityValidate(self):
        validate =  self._dll.agAuthorityValidate()
        print(validate)
        return validate

    def py_agGetAuthorityCode(self):
        return self._dll.agGetAuthorityCode()

    def py_agGetLocalMachineID(self):
        csMachineID = create_string_buffer(b'\x00' * 14)  # 14 字节，包括空字符

        r = self._dll.agGetLocalMachineID(csMachineID)
        if r == 0:
            print(f"\r\n机器ID: {csMachineID.value.decode()}")
            return csMachineID.value.decode()
        else:
            print(f"硬件加密狗调用失败，错误码：{r}")

    def py_agGenerateMRCode_DR(self):
        cspMRCode = create_string_buffer(b'\x00' * 33)  # 33 字节，包括空字符

        r = self._dll.agGenerateMRCode_DR(cspMRCode)
        if r == 0:
            print(f"\r\n机器码: {cspMRCode.value.decode()}")
            return cspMRCode.value.decode()
        else:
            print(f"硬件加密狗机器码调用失败，错误码：{r}")

    def py_agActivateSoftware(self, RegisterKey):
        RegisterKey = c_char_p(str(RegisterKey).encode('utf-8'))    
        r = self._dll.agActivateSoftware(RegisterKey)
        print(r)
        return r    



if __name__ == '__main__':
    a = PyAgDongle()
    b = PyAgDongle2()
    # a.py_agCreateCAgDongle2()
    # c,d = a.py_agGenerateMRCode_DR2()
    # print(c)
    # print(d)
    # e = '16A0D6A0D6A0D6A0D6A0D6A0D6A0D6A0'
    # f = a.py_agActivateSoftware2(e)
    # g = a.py_agAuthorityValidate2()